#include "first_note_dialog.h"

#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>

#include "core/music_settings.h"

FirstNoteDialog::FirstNoteDialog(MusicSettings *settings, QWidget *parent):
  QDialog(parent),
  settings(settings) {
  setWindowTitle("Degré de la première note");
  QGroupBox *firstNoteBox = new QGroupBox("Degré de la première note", this);
  QVBoxLayout *firstNoteBoxLayout = new QVBoxLayout;
  firstNoteBox->setLayout(firstNoteBoxLayout);

  undefined_degree = new QRadioButton("Pas de contrainte pour la première note",
                                      this);
  firstNoteBoxLayout->addWidget(undefined_degree);

  tonic = new QRadioButton("Tonique (1er degré)", this);
  firstNoteBoxLayout->addWidget(tonic);

  supertonic = new QRadioButton("Sus-tonique (2ème degré)", this);
  firstNoteBoxLayout->addWidget(supertonic);

  mediant = new QRadioButton("Médiante (3ème degré)", this);
  firstNoteBoxLayout->addWidget(mediant);

  subdominant = new QRadioButton("Sous-dominante (4ème degré)", this);
  firstNoteBoxLayout->addWidget(subdominant);

  dominant = new QRadioButton("Dominante (5ème degré)", this);
  firstNoteBoxLayout->addWidget(dominant);

  submediant = new QRadioButton("Sus-dominante (6ème degré)", this);
  firstNoteBoxLayout->addWidget(submediant);

  leading = new QRadioButton("Sensible (7ème degré)", this);
  firstNoteBoxLayout->addWidget(leading);

  // boutons

  QPushButton *okButton = new QPushButton("&Valider", this);
  connect(okButton, SIGNAL(clicked()), this, SLOT(validate()));

  QPushButton *cancelButton = new QPushButton("&Annuler", this);
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));

  QHBoxLayout *buttonLayout = new QHBoxLayout;
  buttonLayout->addStretch();
  buttonLayout->addWidget(okButton);
  buttonLayout->addWidget(cancelButton);

  //

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->addWidget(firstNoteBox);
  mainLayout->addLayout(buttonLayout);
  setLayout(mainLayout);

  initialize();
}

/*!
 * Initialise le bon bouton en fonction des paramètres
 */
void FirstNoteDialog::initialize() {
  switch(settings->firstNote) {
    case MusicSettings::UNDEFINED_DEGREE:
      undefined_degree->setChecked(true);
      return;
    case MusicSettings::TONIC:
      tonic->setChecked(true);
      return;
    case MusicSettings::SUPERTONIC:
      supertonic->setChecked(true);
      return;
    case MusicSettings::MEDIANT:
      mediant->setChecked(true);
      return;
    case MusicSettings::SUBDOMINANT:
      subdominant->setChecked(true);
      return;
    case MusicSettings::DOMINANT:
      dominant->setChecked(true);
      return;
    case MusicSettings::SUBMEDIANT:
      submediant->setChecked(true);
      return;
    case MusicSettings::LEADING:
      leading->setChecked(true);
      return;
    default:
      return;
  }
}

/*!
 * Change les paramètres du degré de la première note en fonction du bouton
 * sélectionné puis ferme la fenêtre
 */
void FirstNoteDialog::validate() {
  if(undefined_degree->isChecked())
    settings->firstNote = MusicSettings::UNDEFINED_DEGREE;
  else if(tonic->isChecked())
    settings->firstNote = MusicSettings::TONIC;
  else if(supertonic->isChecked())
    settings->firstNote = MusicSettings::SUPERTONIC;
  else if(mediant->isChecked())
    settings->firstNote = MusicSettings::MEDIANT;
  else if(subdominant->isChecked())
    settings->firstNote = MusicSettings::SUBDOMINANT;
  else if(dominant->isChecked())
    settings->firstNote = MusicSettings::DOMINANT;
  else if(submediant->isChecked())
    settings->firstNote = MusicSettings::SUBMEDIANT;
  else if(leading->isChecked())
    settings->firstNote = MusicSettings::LEADING;

  close();
}
